package androidapp.teacherlist;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.Scanner;

public class TeacherList {
	private Hashtable<String, Teacher> teachers;

	/**
	 * @return the teachers
	 */
	public TeacherList(String TeacherText) {
		super();
		parseText(TeacherText);

	}

	public TeacherList(File file) throws FileNotFoundException {
		super();
		StringBuilder text = new StringBuilder();
		String NL = System.getProperty("line.separator");
		Scanner scanner = new Scanner(file);
		try {
			while (scanner.hasNextLine()) {
				text.append(scanner.nextLine() + NL);
			}
		} finally {
			scanner.close();
		}

		parseText(text.toString());
	}

	private final String TEACHER_OBJECT_SPLITTER = "'";
	private final String ARGUMENT_SPLITTER = ", ";

	private void parseText(String teacherText) {
		ArrayList<String> Teachers = (ArrayList<String>) Arrays
				.asList(teacherText.split(TEACHER_OBJECT_SPLITTER));
		for (String thisTeacher : Teachers) {
			Scanner ScanTeacher = new Scanner(thisTeacher);
			String TeacherName = ScanTeacher.nextLine();
			String TeacherDepartment = ScanTeacher.nextLine();
			ArrayList<String> planningPeriods = (ArrayList<String>) Arrays
					.asList(ScanTeacher.nextLine());
			ArrayList<String> TeacherClubsSponsored = (ArrayList<String>) Arrays
					.asList(ScanTeacher.nextLine().split(ARGUMENT_SPLITTER));
			ArrayList<String> TeacherSportsCoached = (ArrayList<String>) Arrays
					.asList(ScanTeacher.nextLine().split(ARGUMENT_SPLITTER));
			String TeacherEmail = ScanTeacher.nextLine();
			String TeacherWebsite = ScanTeacher.nextLine();

			Teacher teach = new Teacher(TeacherName, TeacherDepartment,
					TeacherClubsSponsored, TeacherSportsCoached, TeacherEmail,
					TeacherWebsite, planningPeriods);
			getTeachers().put(thisTeacher, teach);
		}
	}

	public Hashtable<String, Teacher> getTeachers() {
		if (teachers == null) {
			teachers = new Hashtable<String, Teacher>();
		}
		return teachers;
	}

	/**
	 * @param teacers
	 *            the teachers to set
	 */

}
